<?php

if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class Tfn extends MY_Controller {

	var $path = '';

	function __construct() {
		parent::__construct();
		$this->require_login();
		$this->load->model('tfn_model', 'tfn');
		$this->path = '/recording/IVR/';
		
 	}

	function index() 
	{
		check_permission("tfn", "view");
		
		$data = array();
		$data['page_heading'] = 'TFN';
		$GET = $this->input->get(NULL, true);
 		$this->config->set_item('enable_query_strings', TRUE);
		$config['base_url'] = base_url().$this->uri->segment(1).'/?';
		$config['per_page'] = '20';
 		$config['num_links'] = 5;	
		$start = $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$results = $this->tfn->getListingData($GET,$start);
		$data['start'] = $start;
		$data['results'] = $results['results'];
		$data['total_record'] = $results['total_record'];
		
		if(is_array(@$GET))	{
			unset($GET['per_page']);
 			$qs = http_build_query($GET); 
		} else {
			$qs = '';	
		}
		
		if(isset($qs) && !empty($qs)){
		   $this->pagination->suffix = "&".$qs;	
		}
		
		$config['total_rows'] = $data['total_record'];
		$config['full_tag_open'] = '<ul class="paging">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		
		$perPage = $config['per_page'];
		$this->config->set_item('enable_query_strings', TRUE);
		$this->pagination->initialize($config);
 		
		$this->load->view($this->uri->segment(1).'/listing', $data);
	}
	
	function save($id = 0) 
	{
		$id = (int)$id;
		
		if( $id > 0 ) {
			check_permission("tfn", "edit");
		} else {
			check_permission("tfn", "add");
		}
		
		$POST = $this->input->post(NULL, TRUE);
		$data = array();
		$data['page_heading'] = "TFN"; //($id > 0) ? "Edit TFN" : "Add TFN";
		$data['all_skills'] = getAllskills();
	
		$this->form_validation->set_rules('tfn', 'TFN', 'trim|required|integer|callback_check_duplicate['.$id.']|xss_clean');
		$this->form_validation->set_rules('skill_id', 'Skill', 'trim|required|xss_clean');
		$this->form_validation->set_rules('asterisk_ext', 'Route Extension', 'trim|required|xss_clean');
		$this->form_validation->set_rules('asterisk_priority', 'Route Priority', 'trim|required|xss_clean');
		$this->form_validation->set_rules('active', 'Active', 'trim|required|xss_clean');
		
		if ($this->form_validation->run() == FALSE)
		{
			if ( $id > 0 )
			{
				$detail = $this->tfn->getDetailByID( $id );
				foreach($detail as $k=>$v):
				$data[$k] = $v;
				endforeach;
				$data['id'] = $id;
 			}// end if
 			$this->load->view($this->uri->segment(1).'/save', $data);
		} 
		else 
		{
				$record	= array( 
 							'tfn' 					=> addslashes($this->input->post('tfn')),
							'avaya_vdn' 		 	=> addslashes($this->input->post('tfn')),
							'skill_id' 			 	=> intval($this->input->post('skill_id')),
							'asterisk_ext' 		 	=> addslashes($this->input->post('asterisk_ext')),
							'asterisk_priority'  	=> addslashes($this->input->post('asterisk_priority')),
							'active' 				=> addslashes($this->input->post('active')),
						  );	
			if( $id > 0 ) 
			{
				$record['id'] = $id;	
				$this->tfn->update($record);
				$this->session->set_flashdata('userMesg', 'Record has been updated successfully.');
			} 
			else 
			{
				$this->tfn->add($record);					  		
				$this->session->set_flashdata('userMesg', 'Record has been added successfully.');
			}				
			
			redirect($this->uri->segment(1).'/');
		}
	}// end save
  
  	function batch_update() 
	{
		redirect($this->uri->segment(1).'/');
		check_permission("tfn/batch_update", "edit");
		
		$data = array();
		$data['page_heading'] = "Batch Update TFN Skill"; 
		$data['vdn_skills'] = $this->tfn->getDistinctSkills();
		
 		$this->form_validation->set_rules('skillset', 'Skill', 'trim|required|xss_clean');
		
		
		$this->form_validation->set_rules('off_hours', 'Off hours', 'trim|callback_offhour_times|xss_clean');
		$this->form_validation->set_rules('weekend_off_hours', 'Weekend Off hours', 'trim|callback_offhour_times|xss_clean');
		
		$this->form_validation->set_rules('off_hours_playback', 'Off Hour File Name', 'trim|callback_playback_validation|xss_clean');
		$this->form_validation->set_rules('business_hours_playback', 'Business Off Hour File Name', 'trim|callback_playback_validation|xss_clean');
		
		//$this->form_validation->set_rules('oh_hours1', 'Off hours', 'trim|required|callback_offhour_times|xss_clean');
		//$this->form_validation->set_rules('oh_hours2', 'Off hours', 'trim|xss_clean');
		//$this->form_validation->set_rules('oh_minutes1', 'Off hours', 'trim|xss_clean');
		//$this->form_validation->set_rules('oh_minutes2', 'Off hours', 'trim|xss_clean');
		//$this->form_validation->set_rules('off_hours_playback', 'Off Hour File Name', 'trim|required|xss_clean');
		
		if( $this->input->post('sbmt') == 1 ) 
		{
			if ($this->form_validation->run() == FALSE)
			{
				$this->load->view($this->uri->segment(1).'/'.$this->uri->segment(2), $data);
			} 
			else
			{
				$POST = $this->input->post(NULL, TRUE);
				if( trim($POST['off_hours']) != '' || trim($POST['weekend_off_hours']) != ''  || trim($POST['off_hours_playback']) != ''  || trim($POST['business_hours_playback']) != '' )
				{
					$this->tfn->batch_update($POST);	
					$this->session->set_flashdata('userMesg', 'Record(s) has been updated successfully.');
				
				}
				else
				{
					$this->session->set_flashdata('userMesg', 'Please select atleast one field to update.');
					$this->session->set_flashdata('error_class', '1');
				}
				redirect($this->uri->segment(1).'/'.$this->uri->segment(2));	
			}
		}
		else
		{
			$this->load->view($this->uri->segment(1).'/'.$this->uri->segment(2), $data);
		}
			
	} // end batch_update
  
  
  	
  	function offhour_times($off_hours)
    {
		$off_hours = trim($off_hours);
		if( $off_hours != '') 
		{
			$pattern = '/^([01]?[0-9]|2[0-3]):[0-5][0-9]-([01]?[0-9]|2[0-3]):[0-5][0-9]$/';
			if( preg_match($pattern, $off_hours) ) 
			{
				return true;
			}
			$this->form_validation->set_message('offhour_times', 'Please enter valid %s.');
			return false;
		}
	}
  
  	function playback_validation($filename)
	{
		$path = $this->path;
		$filename = trim($filename);
		if( $filename != '') 
		{
			if( ! file_exists($path.$filename.".wav") ) 
			{
				$this->form_validation->set_message('playback_validation', 'The %s does not exist.');
				return false;
			}
			else
			{
				return true;	
			}
		}
	}
  
  
 	function check_duplicate($tfn, $id ="")
    {
		$id = (int)$id;
		$where = " tfn  = '".mysql_real_escape_string($tfn)."' ";
		if( $id > 0 ) {
			$where .= " AND id <> ".$id	;
		}
 		if(! $this->common_model->record_exist("vdn", $where))
		{
			return true;	
		}
		else
		{
			$this->form_validation->set_message('check_duplicate', 'The %s Must be Unique.');
			return false;
		}
    }// end check_duplicate

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */